package com.ajax.servlet;

import com.ajax.pojo.Student;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wzm
 * @create 2023-11-22 15:17
 * project_name:web2310
 * @Description
 */
@WebServlet("/add")
public class AddAllServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/json;charset=UTF-8");

        //获取请求的数据
        BufferedReader reader = req.getReader();
        String s = reader.readLine();

        //调用service  add(Student student)

        //字符串转java对象
        Student student = JSON.parseObject(s, Student.class);
        System.out.println("student = " + student);

        //studentService.add(student)
        if(student != null){
            resp.getWriter().write("success");
        }else{
            resp.getWriter().write("error");
        }




    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }
}
